/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.appweb;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.responseReturn.entity.ResultObject;
import com.jeesite.common.responseReturn.utils.CommonUtils;
import com.jeesite.common.responseReturn.web.Render;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtColor;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.entity.ZtSpecifications;
import com.jeesite.modules.goods.entity.ZtTrolley;
import com.jeesite.modules.goods.service.ZtColorService;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.goods.service.ZtSpecificationsService;
import com.jeesite.modules.goods.service.ZtTrolleyService;
import com.jeesite.modules.test.entity.TestDataChild;
import com.jeesite.modules.test.service.TestDataService;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;
import com.jeesite.modules.xcxAdmin.entity.ZtGoodsqcode;
import com.jeesite.modules.xcxAdmin.service.ZtGoodsqcodeService;

/**
 * 购物车Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${frontPath}/ztTrolley")
public class AppZtTrolleyController extends BaseController {

	@Autowired
	private ZtTrolleyService ztTrolleyService;
	
	@Autowired
	private ZtGoodsService ztGoodsService;
	
	@Autowired
	private ZtColorService ztColorService;
	@Autowired
	private TestDataService testDataService;
	@Autowired
	private ZtGoodsqcodeService ztGoodsqcodeService;
	@Autowired
	private ZtSpecificationsService ztSpecificationsService;
	@Autowired
	private MyProps myProps;

	/**
	 * 查询列表数据
	 */
	
	@RequestMapping(value = "listData")
	@ResponseBody
	public void listData( HttpServletRequest request,
			HttpServletResponse response,Model model) {
	
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String adminid = jo.getString("adminid");
		String pageNum ="1";
		
		if(jo.get("pageNum")!=null){
			pageNum=jo.get("pageNum").toString();
		}
		
		ZtTrolley ztTrolley=new ZtTrolley();
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztTrolley.setZtAdmin(ztAdmin);
		ztTrolley.setPageNum((Integer.valueOf(pageNum)-1)*15);
		List<ZtTrolley> page = ztTrolleyService.findMh(ztTrolley);
		
		List<ZtTrolley> ztList = new ArrayList<ZtTrolley>();
				
		for (ZtTrolley zt : page) {
			
			ZtGoods zd = zt.getZtGoods();
			
			if(StringUtils.isNotBlank(zd.getImg().toString())&&!zd.getImg().toString().startsWith("http")){
				zd.setImg(myProps.getServerPath()+zd.getImg().toString());
			}
			
			if(StringUtils.isNotBlank(zt.getZtColor().getImg())){
				if(!zd.getImg().toString().startsWith("http")){
				zd.setImg(myProps.getServerPath()+zt.getZtColor().getImg());
				}
			}else{
				zd.setImg(zd.getImg());
			}
			
			String level=StringUtils.isBlank(zt.getZtAdmin().getLevel())?"0":zt.getZtAdmin().getLevel();
			if("0".equals(level)){
				zd.setPrice(zt.getZtGoods().getCommonPrice());
			}else{
				zd.setPrice(zt.getZtGoods().getDiscountPrice());
			}

			if(zt.getSpecifications()!=null && StringUtils.isNotBlank(zt.getSpecifications().getId())&&!"0".equals(zt.getSpecifications().getId())){
				ZtSpecifications ztSpecifications=ztSpecificationsService.get(zt.getSpecifications());
				if(ztSpecifications!=null && StringUtils.isNotBlank(ztSpecifications.getId())){
				zt.setGuige(zt.getZtColor().getTitle()+"、"+ztSpecifications.getName());
				}
			}else{
				zt.setGuige(zt.getZtColor().getTitle());
			}
			
			zt.setZtGoods(zd);
		
			ztList.add(zt);
		}
		
		
		ResultObject res = new ResultObject();
		
		res.setResult(200L);
		res.setData(page);
		Render.renderString(response, res);
		
	}

	/**
	 * 保存数据
	 */

	@PostMapping(value = "save")
	@ResponseBody
	public void save(HttpServletRequest request,
			HttpServletResponse response) {
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String adminid = jo.getString("adminid");
		String goodsid = jo.getString("goodsid");
		String colorid = jo.getString("colorid");
		String numCount = "1";
		if(jo.get("numCount")!=null){
			numCount=jo.get("numCount").toString();
		}
		ZtTrolley ztTrolley=new ZtTrolley();
		
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztTrolley.setZtAdmin(ztAdmin);
		
		ZtGoods ztGoods=new ZtGoods(); 
		ztGoods.setId(goodsid);
		ztTrolley.setZtGoods(ztGoods);
		
		ZtColor ztColor=new ZtColor();
		ztColor.setId(colorid);
		
		ztTrolley.setZtColor(ztColor);
		
		
		String specificationsid = "";
		if(jo.get("specificationsid")!=null){
			specificationsid=jo.get("specificationsid").toString();
			ZtSpecifications specifications=new ZtSpecifications();
			specifications.setId(specificationsid);
			
			specifications=ztSpecificationsService.get(specifications);
			ztTrolley.setSpecifications(specifications);
			
			ztGoods.setCode(specifications.getSku());
			ztGoods=ztGoodsService.getByCode(ztGoods);
			if(ztGoods!=null && StringUtils.isNotBlank(ztGoods.getId())){
				ztTrolley.setZtGoods(ztGoods);//couponCode
			}
		}else{
			ztColor=ztColorService.get(ztColor);
			ztGoods.setCode(ztColor.getSku());
			ztGoods=ztGoodsService.getByCode(ztGoods);
			if(ztGoods!=null && StringUtils.isNotBlank(ztGoods.getId())){
				ztTrolley.setZtGoods(ztGoods);
			}
		}
		
		ZtTrolley ztro=ztTrolleyService.getByGoods(ztTrolley);
		
		if(ztro==null){
			ztTrolley.setNumCount(Integer.valueOf(numCount));
			ztTrolley.setCreateDate(new Date());
			ztTrolleyService.save(ztTrolley);
		}else{
			ztro.setNumCount(Integer.valueOf(numCount)+ztro.getNumCount());
			ztro.setCreateDate(new Date());
			ztTrolleyService.save(ztro);
		}
		
		ResultObject res = new ResultObject();
		int count=ztTrolleyService.getCount(ztTrolley);
		res.setResult(200L);
		res.setData(count);
		Render.renderString(response, res);
		
	}
	
	@PostMapping(value = "getTrolleyCount")
	@ResponseBody
	public void getTrolleyCount(HttpServletRequest request,
			HttpServletResponse response) {
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String adminid = jo.getString("adminid");
	
		ZtTrolley ztTrolley=new ZtTrolley();
		
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztTrolley.setZtAdmin(ztAdmin);
		
		ResultObject res = new ResultObject();
		int count=ztTrolleyService.getCount(ztTrolley);
		res.setResult(200L);
		res.setData(count);
		Render.renderString(response, res);
		
	}
	
	/**
	 * 保存数据
	 */

	@PostMapping(value = "updateCount")
	@ResponseBody
	public void updateCount(HttpServletRequest request,
			HttpServletResponse response) {
		
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		String trolleyid = jo.getString("trolleyid");
		String adminid = jo.getString("adminid");
		String numCount = jo.getString("numCount");
		
		ZtTrolley ztTrolley=new ZtTrolley();
		
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztTrolley.setZtAdmin(ztAdmin);
		
		ztTrolley.setId(trolleyid);
		ztTrolley=ztTrolleyService.get(ztTrolley);
		ztTrolley.setNumCount(Integer.valueOf(numCount));
		ztTrolleyService.save(ztTrolley);
		
		ResultObject res = new ResultObject();
		int ztTrolleyCount=ztTrolleyService.getCount(ztTrolley);
		res.setResult(200L);
		res.setData(ztTrolleyCount);
		Render.renderString(response, res);
		
	}
	
	/**
	 * 删除数据
	 */

	@PostMapping(value = "delete")
	@ResponseBody
	public void delete(HttpServletRequest request,
			HttpServletResponse response) {
		String paramStr=CommonUtils.getParams(request);
		
		JSONObject jo=JSONObject.parseObject(paramStr);
		
		String adminid = jo.getString("adminid");
		
		JSONArray ja = jo.getJSONArray("trolleyid");
		
		for(int i=0;i<ja.size();i++){
			String trolleyid=ja.get(i).toString();
			ZtTrolley ztTrolley=new ZtTrolley();
			ztTrolley.setId(trolleyid);
			ztTrolleyService.delete(ztTrolley);
		}
		
		ResultObject res = new ResultObject();
		
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ZtTrolley zt=new ZtTrolley();
		zt.setZtAdmin(ztAdmin);
				
		int ztTrolleyCount=ztTrolleyService.getCount(zt);
		
		res.setResult(200L);
		res.setData(ztTrolleyCount);
		Render.renderString(response, res);
		
	}

}